var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    page_data: {
      type: Array,
      value: [],
      observer: function (newV) {
        if (newV && newV.length >= 1) {
          let listData = this.data.listData.concat(newV)
          this.setData({
            listData
          })
        }
      }
    }
  },


  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {
      this.setData({
        index:0,
        password_show:false,
        show_pay: false,
        show_dialog: false,
        pay_index:0,
        is_has:false
      })
      this.getData();
    },
    hide() {},
    resize() {},
  },
  lifetimes: {
    ready() {
      this.getData();
      this.get_buy_data();;
      this._getBalancePwdInfo()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    count: "",
    subscribe_title: "",
    activity_id: '',
    datas:{},
    list: [{
        label: '全部',
        status: null
      },
      {
        label: '未开始',
        status: 0
      },
      {
        label: '进行中',
        status: 1
      },
      {
        label: '已成功',
        status: 2
      },
      {
        label: '已失败',
        status: 44
      }
    ],
    current_index: 0,
    listData: [],
    sex: ["会员", '管理员'],
    type: ['登录密码', '支付密码'],
    index: 0,
    pay_index: 0,
    role: "member",
    is_admin: false,
    require_pwd: false,
    show_pay: false,
    show_dialog: false,
    password_open: true,
    password: '',
    active_value:0,
    current_page: 1,
    last_page: 1,
    status: null,
    password_show:false,
    is_has:false,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _getBalancePwdInfo() {
      let urlStr = app.getNetAddresss("member.balance-password.is-has-password");
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              is_has: res.data.is_has
            });
          } else {
            console.log(res.msg);
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    },
    handleInput(event) {
      this.setData({
        count: event.detail.value
      });
    },
    closeDialog() {
      this.setData({
        show_dialog: false
      })
    },
    sigin_handle(type) {
      let json = {
        stage_id: this.data.activity_id,
        amount: this.data.count,
        admin: this.data.index
      }
      if (this.data.index==0 && this.data.require_pwd ==1 )  {
          json.pwd_type = this.data.pay_index == 0 ? 'login' : 'pay';
          json.pwd = this.data.password;
      }

      app._getNetWork({
        url: app.getNetAddresss("plugin.sign-buy.frontend.user-sign.submit"),
        data: json,
        success: (resdata) => {
          let {
            result,
            msg,
            data
          } = resdata.data;
          if (result == 0)  {
            if (this.data.index==0 && this.data.require_pwd ==1  && this.data.pay_index==1)  {
              if(data.password_error==1){
                this.setData({password_show:true});
              }else{
                app.tips(msg)
              }
          }else{
            app.tips(msg)
          }
            return
          };
          app.tips(msg);
          this.getData()
          this.get_buy_data()
          this.setData({ show_dialog: false,index:0,amount:"",password:"",show_pay: false})
        }
      })

    },
    onClickHide() {
      this.setData({ password_show: false });
    },
    password_handle(e){
      let type = e.currentTarget.dataset.type;
      if(type == 'forget'){
        if (this.data.is_has) {
          //修改支付密码
          wx.navigateTo({
            url: "/packageA/member/balance_password/balance_password"
          })
        } else {
          //设置支付密码
          wx.navigateTo({
            url: "/packageA/member/set_balance_password/set_balance_password"
          })
        }
      }else {
        this.setData({password_show:false});
      }
    },
    finish(){
      this.getData()
    },
    confirm(e) {
      let {
        type
      } = e.currentTarget.dataset;
      if (type == 'subscribe') {
        if (this.data.count <= 0) {
          return app.tips("请输入有效签到认购数量")
        }
        if (this.data.is_admin || this.data.require_pwd=='0') {
          this.sigin_handle('subscribe')

        } else {
          if (this.data.password_open) {
            this.setData({
              show_pay: true
            })
          }
        }

      }
      if (type == 'pay') {
        if (this.data.password == '') {
          return app.tips(`请输入${this.data.pay_index==0?'登录':'支付'}密码`)
        }
        this.sigin_handle('pay');
      }
    },
    handle_password(e) {
      this.setData({
        password: e.detail.value
      })
    },
    closePay() {
      this.setData({
        show_pay: false
      })
    },
    choose(e) {
      let {
        i,
        type
      } = e.currentTarget.dataset;
      if (type == 'member') {
        this.setData({
          index: i,
          role: i == 1 ? 'admin' : 'member'
        })
      } else {
        this.setData({
          pay_index: i
        })
      }
    },
    sign_list_data(json) {
      app._getNetWork({
        url: app.getNetAddresss('plugin.sign-buy.frontend.sign-buy-page.activity-list'),
        data: json,
        success: (resdata) => {
          let {
            result,
            data,
            msg
          } = resdata.data;
          if (result == 0) return app.tips(msg);
          this.setData({
            listData: data.data,
          })

          this.triggerEvent("more_data_page", {
            current_page: data.current_page,
            last_page: data.last_page,
            status: this.data.status
          })
        }
      })
    },
    get_buy_data() {
      app._getNetWork({
        url: app.getNetAddresss("plugin.sign-buy.frontend.user-sign.index"),
        success: (resdata) => {
          let {
            result,
            msg,
            data: {
              active_value,
              is_admin,
              require_pwd
            }
          } = resdata.data;
          if (result == 0) return this.$toast(msg);
          this.setData({
            active_value,
            require_pwd,
            is_admin
          })
        }
      })
    },
    getData() {
      this.sign_list_data()

      app._getNetWork({
        url: app.getNetAddresss("plugin.sign-buy.frontend.sign-buy-page.home"),
        success: (resdata) => {
          let {
            data,
            result,
            msg
          } = resdata.data;
          console.log(data)
          if (result == 0) {
            return app.tips(msg)
          }
          let {
            carousel_h5_link,
            banner_h5_link,
            banner_thumb_url,
            carousel_imgs_url,
            sign_multiple,
            activity_list
          } = data;
          this.setData({
            carousel_h5_link,
            banner_h5_link,
            banner_thumb_url,
            carousel_imgs_url,
            sign_multiple,
            activity_list
          })

        }
      })
    },
    sign_subscribe_handle(e) {
      let data = e.detail.item;
      this.setData({
        datas:data,
        show_dialog: true,
        subscribe_title: data.activity_name,
        activity_id: data.stage_id,
        count:"",
        password:"",
      })
    },
    handle(e) {
      let status = e.currentTarget.dataset.status,
        json = {},
        current_index = e.currentTarget.dataset.index;
      this.setData({
        current_index,
        status
      })

      if (status !== null) {
        json.status = status
      }
      this.sign_list_data(json)
    }
  }
})